package com.ruoyi.web.controller.module.points;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.points.EstatePoints;
import com.ruoyi.system.domain.module.points.EstatePointsRecord;
import com.ruoyi.system.domain.module.wx.WxUser;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.system.service.module.points.IEstatePointsRecordService;
import com.ruoyi.system.service.module.points.IEstatePointsService;
import com.ruoyi.system.service.module.property.IPropertyService;
import com.ruoyi.system.service.module.wx.IWxUserService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 积分Controller
 *
 * @author ruoyi
 * @date 2025-05-30
 */
@Validated
@Api(value = "积分控制器", tags = {"积分管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/points")
public class EstatePointsController extends BaseController {

    private final IEstatePointsService pointsService;
	private final IEstateService estateService;
	private final IPropertyService propertyService;
	private final IWxUserService wxUserService;
	private final IEstatePointsRecordService pointsRecordService;

	@Value("${ruoyi.imgUrl}")
	private String imgUrl;

    /**
     * 查询积分列表
     */
    @ApiOperation("查询积分列表")
    @PreAuthorize("@ss.hasPermi('module:points:list')")
    @GetMapping("/list")
    public TableDataInfo<EstatePoints> list(@Validated EstatePoints estatePoints) {
		SysUser sysUser = getUser();
        QueryWrapper<EstatePoints> qw = new QueryWrapper<>();
		if(sysUser.getUserType() == 2){
			qw.eq("property_id",sysUser.getPropertyId());
			if (sysUser.getEstateId()!=null){
				qw.eq("estate_id",sysUser.getEstateId());
			}
		} else if(sysUser.getUserType() == 3){
			qw.eq("estate_id",sysUser.getEstateId());
		}else{
			if(estatePoints.getPropertyId() != null){
				qw.eq("property_id", estatePoints.getPropertyId());
			}
			if(estatePoints.getEstateId() != null){
				qw.eq("estate_id", estatePoints.getEstateId());
			}
		}
        TableDataInfo<EstatePoints> td = PageUtils.buildDataInfo(pointsService.page(PageUtils.buildPage(),qw));
        List<EstatePoints> list = td.getRows();
		for (EstatePoints list1:list) {
			if (list1.getPropertyId() != null) {
				list1.setPropertyName(propertyService.findById(list1.getPropertyId()).getPropertyName());
			}
			if (list1.getEstateId() != null) {
				list1.setEstateName(estateService.findById(list1.getEstateId()).getEstateName());
			}
			if (list1.getWxUserId()!= null){
				WxUser wxUser = wxUserService.findById(list1.getWxUserId());
				list1.setWxUserName(wxUser.getNickName());
				list1.setFaceUrl(imgUrl+wxUser.getFaceUrl());
			}
		}
        return td;
    }

    /**
     * 导出积分列表
     */
    @ApiOperation("导出积分列表")
    @PreAuthorize("@ss.hasPermi('module:points:export')")
    @Log(title = "积分", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EstatePoints> export(@Validated EstatePoints estatePoints) {
        List<EstatePoints> list = pointsService.list();
        ExcelUtil<EstatePoints> util = new ExcelUtil<EstatePoints>(EstatePoints.class);
        return util.exportExcel(list, "积分");
    }

    /**
     * 获取积分详细信息
     */
    @ApiOperation("获取积分详细信息")
    @PreAuthorize("@ss.hasPermi('module:points:query')")
    @GetMapping("/{id}")
    public AjaxResult<EstatePoints> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstatePoints estatePoints = pointsService.getById(id);
        return AjaxResult.success(estatePoints);
    }

    /**
     * 新增积分
     */
    @ApiOperation("新增积分")
    @PreAuthorize("@ss.hasPermi('module:points:add')")
    @Log(title = "积分", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EstatePoints estatePoints) {
        return toAjax(pointsService.save(estatePoints) ? 1 : 0);
    }

    /**
     * 修改积分
     */
    @ApiOperation("修改积分")
    @PreAuthorize("@ss.hasPermi('module:points:edit')")
    @Log(title = "积分", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping("/update")
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EstatePoints estatePoints) {
        return toAjax(pointsService.updateById(estatePoints) ? 1 : 0);
    }

    /**
     * 删除积分
     */
    @ApiOperation("删除积分")
	@PreAuthorize("@ss.hasPermi('module:points:edit')")
	@Log(title = "积分", businessType = BusinessType.UPDATE)
	@GetMapping("/delAll")
    public AjaxResult<Void> remove(@Validated EstatePoints estatePoints) {
		SysUser sysUser = getUser();
		QueryWrapper<EstatePoints> qw = new QueryWrapper<>();
		if(sysUser.getUserType() == 2){
			qw.eq("property_id",sysUser.getPropertyId());
			if (sysUser.getEstateId()!=null){
				qw.eq("estate_id",sysUser.getEstateId());
			}
		} else if(sysUser.getUserType() == 3){
			qw.eq("estate_id",sysUser.getEstateId());
		}else{
			if(estatePoints.getPropertyId() != null){
				qw.eq("property_id", estatePoints.getPropertyId());
			}
			if(estatePoints.getEstateId() != null){
				qw.eq("estate_id", estatePoints.getEstateId());
			}
		}
		List<EstatePoints> list = pointsService.list(qw);
		for (EstatePoints list1:list){
			list1.setPointsNum(0L);
			EstatePointsRecord record = new EstatePointsRecord();
			record.setIsAdd(0L);
			record.setEstateId(list1.getEstateId());
			record.setPropertyId(list1.getPropertyId());
			record.setWxUserId(list1.getWxUserId());
			record.setIsDel(1L);
			record.setUsePointsNum(list1.getPointsNum());
			pointsService.updateById(list1);
			pointsRecordService.save(record);
		}
        return toAjax(1);
    }
}
